<template>
  <el-container class="home-container">
    <!-- 头部去 -->
    <el-header>
      <div>
        <img src="../assets/logo.png">
        <span>QiaoFinn装饰</span>
      </div>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="collapse-btn" @click="collapseBtn">||||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF"
         :collapse-transition="false" unique-opened :collapse="isCollapse" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="menu.path" v-for="menu in menulist" :key="menu.id">
            <template slot="title">
              <i :class="menu.icon"></i>
              <span>{{menu.name}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="subMenu.path" v-for="subMenu in menu.children" :key="subMenu.id" @click="saveNavState(subMenu.path)">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{subMenu.name}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区 -->
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>

<script>
  import {
    _menuApi
  } from './request/api.js'
  export default {
    data() {
      return {
        menulist: [], // 菜单数据
        isCollapse: false, // 控制左菜单栏的展开和关闭
        activePath: ''// 被激活的二级菜单地址
      }
    },
    created() {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      async getMenuList() {
        // 获取菜单数据
        const { data: res } = await _menuApi()
        console.log(res)
        this.menulist = res.data
      },
      collapseBtn() {
        // 控制菜单栏的展开
        this.isCollapse = !this.isCollapse
      },
      saveNavState(activePath) {
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #FFFFFF;
    font-size: 20px;

    >div {
      display: flex;
      align-items: center;

      >img {
        height: 60px;
        width: 60px;
        border-radius: 50%;
        margin-right: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    .el-menu {
        border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }
  .iconfont{
    margin-right: 10px;
  }
  .collapse-btn{
    background-color: #4a5064;
    text-align: center;
    padding: 5px 0;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
